/**
 * Created by ajhin on 7/12/2015.
 */
package com.societyGames2.convert
{
import org.as3commons.asblocks.parser.antlr.LinkedListToken;
import org.as3commons.asblocks.parser.antlr.LinkedListTree;

/**
 * Dumps all the information we have about this node.
 */
public class TokenTreeConverter implements ITreeConverter
{
  //ITreeToCSharp-->
  public function convert(tree:LinkedListTree):String
  {
    var source:String = "";

    if (tree.children == null || tree.children.length == 0)
    {
      source += convertLeaf(tree);
    }
    else
    {
      for (var i:int = 0; tree.children != null && i < tree.children.length; i++)
      {
        var childTree:LinkedListTree = LinkedListTree(tree.getChild(i));
        source += TreeConverters.convert(childTree);
      }
    }
    return source;
  }
  //<--ITreeToCSharp

  private function convertLeaf(tree:LinkedListTree):String
  {
    var source:String = convertTokens(tree.getStartToken(), tree.getStopToken());
    return source;
  }

  private function convertTokens(startToken:LinkedListToken, stopToken:LinkedListToken):String
  {
    var source:String = "";
    var currentToken:LinkedListToken = startToken;
    while (currentToken != null)
    {
      source += currentToken.text;
      if (currentToken == stopToken)
      {
        break;
      }
      currentToken = currentToken.getNext();
    }
    return source;
  }
}
}
